﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
    public class HINH_ANHDAL : KetNoiDuLieu
    {
        public void Insert(HINH_ANH hinhanh)
        {
            SqlCommand cm = new SqlCommand("sp_hinhanh_Insert", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA_SP", hinhanh.MA_SP);
            cm.Parameters.AddWithValue("@URL_HINH", hinhanh.URL_HINH);
            cm.ExecuteNonQuery();

        }
        public void Delete(int Ma)
        {
            SqlCommand cm = new SqlCommand("sp_hinhanh_Delete", LayKetNoi());

            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MA", Ma);
            cm.ExecuteNonQuery();
        }
        public HINH_ANHCollection GET(string Where, int PageIndex, int PageSize, string OrderBy,
                                        string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_hinhanh_GET", LayKetNoi());
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);

            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);


            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);

            HINH_ANHCollection hinhanhcol = new HINH_ANHCollection();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                HINH_ANH ha = new HINH_ANH();
                ha.MA = int.Parse(ds.Tables[0].Rows[i]["MA"].ToString());
                ha.MA_SP = int.Parse(ds.Tables[0].Rows[i]["MA_SP"].ToString());
                ha.URL_HINH = ds.Tables[0].Rows[i]["URL_HINH"].ToString();
                hinhanhcol.Add(ha);

            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return hinhanhcol;
        }
    }
}
